//
//  SignViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/6/13.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "SignViewController.h"

@interface SignViewController ()
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic) double timeCount;
@property (nonatomic,strong) MBProgressHUD *hud;

@end

@implementation SignViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self UISetting];
}

- (void)UISetting{
    [_textFieldMobile addTarget:self action:@selector(mobileTextFieldEditing) forControlEvents:UIControlEventEditingChanged];
    
    [_textFieldCode addTarget:self action:@selector(codeTextFieldEditing) forControlEvents:UIControlEventEditingChanged];
}

#pragma mark - textField方法

- (void)mobileTextFieldEditing{
    if ([BGFunctionHelper checkTel:_textFieldMobile.text]) {
        [self unlockedGetCodeButton:_buttonGetCode];
    }else{
        [self lockedGetCodeButton:_buttonGetCode];
    }
}

- (void)codeTextFieldEditing{
    if ([BGFunctionHelper checkNum:_textFieldCode.text]) {
        if (_textFieldCode.text.length == 4) {
            [self unlockedGetCodeButton:_buttonLogin];
        }else{
            [self lockedGetCodeButton:_buttonLogin];
        }
    }else{
        [self lockedGetCodeButton:_buttonLogin];
    }
}

- (void)lockedGetCodeButton:(UIButton *)button{
    button.enabled = NO;
    [button setTintColor:kColorFrom0x(0xbababa)];
    [button setTitleColor:kColorFrom0x(0xbababa) forState:UIControlStateNormal];
}

- (void)unlockedGetCodeButton:(UIButton *)button{
    button.enabled = YES;
    [button setTintColor:kGreenColor];
    [button setTitleColor:kGreenColor forState:UIControlStateNormal];
}

#pragma mark - 点击事件
//获取验证码
- (IBAction)getCodeButtonClick:(UIButton *)sender {
    [self getCodeFunByMobile:_textFieldMobile.text];
}


- (IBAction)loginButtonClick:(UIButton *)sender {
    [self loginFunByMobile:_textFieldMobile.text code:_textFieldCode.text];
}

#pragma mark - logicalFun
//获取验证码
- (void)getCodeFunByMobile:(NSString *)mobile{
    [self.hub hideAnimated:YES];
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    // Set the label text.
    self.hub.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
    [[AFClient shareInstance] getCodeByMobile:mobile progressBlock:nil success:^(id responseBody) {
        NSLog(@"code ->%@",responseBody[@"data"]);
        [self.hub hideAnimated:YES];
        if ([responseBody[@"code"] integerValue] == 200) {
            _timeCount = 90;
            _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeRun) userInfo:nil repeats:YES];
            [_timer setFireDate:[NSDate distantPast]];
            kAlert(@"验证码已发送");
            [_textFieldCode becomeFirstResponder];
            [self getPowerVerson];
        }else{
            kAlert(responseBody[@"message"]);
        }
    } failure:^(NSError *error) {
        [self.hub hideAnimated:YES];
    }];
}

//登录
- (void)loginFunByMobile:(NSString *)mobile code:(NSString *)code{
    [self.hub hideAnimated:YES];
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    // Set the label text.
    self.hub.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
    [[AFClient shareInstance] loginByMobile:mobile code:code progressBlock:nil success:^(id responseBody) {
        [self.hub hideAnimated:YES];
        if ([responseBody[@"code"] integerValue] == 200) {
            NSLog(@"login -> %@",responseBody);
            NSDictionary *dic = responseBody[@"data"];
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"mobile"] forKey:@"mobile"];
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"name"] forKey:@"userName"];
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"id"] forKey:@"userId"];
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"id"] forKey:@"adminUserId"];
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"mobile"] forKey:@"SubMobile"];
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"name"] forKey:@"SubUserName"];
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"city_name"] forKey:@"cityName"];
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"organization_id"] forKey:@"organization_id"];
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"admin"] forKey:@"admin"];
            [[NSUserDefaults standardUserDefaults] setValue:dic[@"admin"] forKey:@"userAdmin"];


//            adminUserId
//            [[NSUserDefaults standardUserDefaults] setValue:dic[@"created"] forKey:@"created"];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:nil];
        }else{
            kAlert(responseBody[@"message"]);
        }
    } failure:^(NSError *error) {
        [self.hub hideAnimated:YES];
    }];
}

- (void)timeRun{
    [self lockedGetCodeButton:_buttonGetCode];
    _timeCount --;
    NSString *title = [NSString stringWithFormat:@"重新获取(%.lfs)",_timeCount];
    _buttonGetCode.titleLabel.text = title;
    [_buttonGetCode setTitle:title forState:UIControlStateNormal];
    if (_timeCount < 1) {
        NSString *title = @"再次发送";
        _buttonGetCode.titleLabel.text = title;
        [_buttonGetCode setTitle:title forState:UIControlStateNormal];
        [_timer setFireDate:[NSDate distantFuture]];
        
        [self unlockedGetCodeButton:_buttonGetCode];
    }
}

- (void)getPowerVerson{
    [[AFClient shareInstance] getNewVersonAboutPowerProgressBlock:nil success:^(id responseBody) {
        if ([responseBody[@"code"] integerValue] == 200) {
            NSString *verson = responseBody[@"data"];
            if (![BGFunctionHelper isNULLOfString:verson]) {
                [[NSUserDefaults standardUserDefaults] setValue:verson forKey:@"powerVerson"];
            }
        }
    } failure:^(NSError *error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
